function is_valid_url(url)
{
	return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}

function checkURL(url) {
    return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}

var i = 0;
var j = 0;
$("textarea#textarea_embed_video").bind("keyup",function(){
	var valueEmbed = $("textarea#textarea_embed_video").val();
	if(valueEmbed!=""){
		if(i==1){
			$("#loading").show().delay(2000);
			$("#loading").fadeIn(400).html('<img src="http://1.bp.blogspot.com/-2M-4z9GkdR4/U-GzWcNt7vI/AAAAAAAABX0/86JuceTdJPk/s1600/ajax-loader.gif">');
			$("#loading").fadeOut('slow');
			setTimeout(function(){
				var url = is_valid_url($("textarea#textarea_embed_video").val());
					if(url==null){
						$("#result_embed_video").html("<b>You have to pass url, eg. https://www.youtube.com/watch?v=27szrI5n4wg</b>");
					}else{
							$("#result_embed_video").oembed(valueEmbed);
					}
			},2500);
		}
		i++;
	}
});
$("textarea#textarea_embed_image").bind("keyup",function(){
	var valueEmbed = $("textarea#textarea_embed_image").val();
	if(valueEmbed!=""){
								var url = is_valid_url($("textarea#textarea_embed_image").val());

								if(url==null){
									$("#result_embed_image").html("<b>You have to pass url, eg. http://www.sambath.com/images/cute-cat.jpg</b>");
								}else{
									var extension=checkURL(valueEmbed);
									if(extension==false){
										$("#result_embed_image").html("<b>You have to pass url, eg. http://www.sambath.com/images/cute-cat.jpg</b>");
									}else{
										$("#result_embed_image").html("<img style='paddin:5px; width:120px; height:120px;' src='"+valueEmbed+"' />");
							
									}
								}	
					}
				});